package com.lkd.job;


import com.lkd.common.VMSystem;
import com.lkd.entity.UserEntity;
import com.lkd.service.UserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class UserJob {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 自动维修工单
     *
     * @param param
     * @return
     */
    @XxlJob("workCountInitJobHandler")
    public ReturnT<String> workCountInitJobHandler(String param) {
        // 获取所有区域的运维人员.
        List<UserEntity> userList = userService.list();
        userList.forEach(u -> {
            if (!u.getRoleCode().equals("1001")) {
                String key = VMSystem.REGION_TASK_KEY_PREF  // 前缀
                        + new SimpleDateFormat("yyyyMMdd").format(new Date()) // 每日时间
                        + "." + u.getRegionId() // 区域id
                        + "." + u.getRoleCode(); // 员工分类
                redisTemplate.opsForZSet().add(key, u.getId().toString(), 0);
                redisTemplate.expire(key, Duration.ofDays(1));
            }
        });
        return ReturnT.SUCCESS;
    }
}
